package com.ax.action.system;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ax.pojo.TabTypes;
import com.ax.service.TabTypesService;
import com.ax.utils.BaseAction;

@Controller("tabTypesAction")
public class TabTypesAction extends BaseAction {

	private TabTypesService tabTypesService;

	@Resource(name = "tabTypesService")
	public void setTabTypesService(TabTypesService tabTypesService) {
		this.tabTypesService = tabTypesService;
	}

	public String findTypes() {
		List<TabTypes> list = tabTypesService.findParentTypes();
		StringBuffer values = new StringBuffer();
		String id=request.getParameter("id");
		values.append("<option value='0'>").append("无(作为一级分类)").append(
				"</option>");
		if (list != null && list.size() > 0) {
			for (TabTypes tabTypes : list) {
				printTypes(values, tabTypes, 0,id);
			}
		}
		request.setAttribute("types", values.toString());
		return "add";
	}

	private void printTypes(StringBuffer values, TabTypes tabTypes, int level,String id) {

		values.append("<option ");
		if (id!=null&&!"".equals(id)) {
			 int i=Integer.parseInt(id);
			 if (i==tabTypes.getTid()) {
				 values.append("selected=\"selected\"");
			}
		}
		values.append("value='" + tabTypes.getTid() + "'>");
		
		for (int i = 0; i < level; i++) {
			values.append("&#12288;");
		}
		if (level > 0) {
			values.append("├");
		}
		values.append(tabTypes.getTname());
		values.append("</option>");
		printType(values, tabTypes, level,id);

	}

	private void printType(StringBuffer values, TabTypes tabTypes, int level,String id) {
		if (tabTypes.getTabTypeses().size() > 0) {
			Set<TabTypes> set = tabTypes.getTabTypeses();
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				TabTypes tp = (TabTypes) iterator.next();
				printTypes(values, tp, level + 1,id);
			}

		}
	}

	public String save() {
		String TParentID = request.getParameter("TParentID");
		String tname = request.getParameter("tname");
		String tkeyword = request.getParameter("tkeyword");
		String tdescribe = request.getParameter("tdescribe");
		TabTypes types = new TabTypes();
		types.setTdescribe(tdescribe);
		types.setTname(tname);
		types.setTkeyword(tkeyword);
		if (!"0".equals(TParentID)) {
			TabTypes tp = tabTypesService.getParentTypes(TParentID);
			types.setTabTypes(tp);

		}
		tabTypesService.saveTypes(types);
		return "add_lists";
	}
	
	
	public String update() {
		String id=request.getParameter("id");
		String TParentID = request.getParameter("TParentID");
		String tname = request.getParameter("tname");
		String tkeyword = request.getParameter("tkeyword");
		String tdescribe = request.getParameter("tdescribe");
		TabTypes types =tabTypesService.getParentTypes(id);
		types.setTdescribe(tdescribe);
		types.setTname(tname);
		types.setTkeyword(tkeyword);
		if (!"0".equals(TParentID)) {
			if (!id.equals(TParentID)) {
				TabTypes tp = tabTypesService.getParentTypes(TParentID);
				types.setTabTypes(tp);
			}
		}
		tabTypesService.updateType(types);
		return "add_lists";
	}
	public String del() {
		String id=request.getParameter("id");
		TabTypes tabTypes=tabTypesService.getParentTypes(id);
		tabTypesService.delType(tabTypes);
		return "add_lists";
	}

	public String findTypesMain() {
		List<TabTypes> list = tabTypesService.findParentTypes();
		StringBuffer values = new StringBuffer();
		for (TabTypes tabTypes : list) {
			printTypesMain(values, tabTypes, 0);
		}
		
		request.setAttribute("main",values.toString());
		return "list";
	}

	private void printTypesMain(StringBuffer values, TabTypes tabTypes,
			int level) {

		values.append("<tr>");
		values
				.append("	<td  class=\"css_list\">" + tabTypes.getTid()
						+ "</td>");
		String str = "";
		for (int i = 0; i < level; i++) {
			str += "&#12288;";
		}
		if (level > 0) {
			str += "├";
		}
		values
				.append("<td class=\"css_list\" align=\"center\"><div align=\"left\"><strong>"
						+ str
						+ tabTypes.getTname()
						+ "</strong> <font color=\"#999999\"></font></div></td>");
		values
				.append("<td class=\"css_list\"><input type=\"button\" value=\"添加\" onclick=\"location.href='typesAction!findTypes.do?id="+tabTypes.getTid()+"';\" name=\"modify\">");
		values
				.append("<input type=\"button\" value=\"修改\" onclick=\"location.href='typesAction!getTypes.do?id="+tabTypes.getTid()+"';\" name=\"modify2\">");
		values
				.append("<input type=\"button\" value=\"删除\" onclick=\"if(confirm('您确定要删除这条记录吗?')){location.href='typesAction!del.do?id="+tabTypes.getTid()+"';}\" name=\"delete\">");
		values.append("</td>");
		values.append("</tr>");
		printTypeMain(values, tabTypes, level);

	}

	private void printTypeMain(StringBuffer values, TabTypes tabTypes, int level) {
		if (tabTypes.getTabTypeses().size() > 0) {
			Set<TabTypes> set = tabTypes.getTabTypeses();
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				TabTypes tp = (TabTypes) iterator.next();
				printTypesMain(values, tp, level + 1);
			}

		}
	}
	
	public String getTypes(){
		List<TabTypes> list = tabTypesService.findParentTypes();
		StringBuffer values=new StringBuffer();
	    String id=request.getParameter("id");
		values.append("<option value='0'>").append("无(作为一级分类)").append(
				"</option>");
			for (TabTypes tabTypes : list) {
				printTypes(values, tabTypes, 0,id);
			}

		TabTypes tabTypes=tabTypesService.getParentTypes(id);
		request.setAttribute("values", values);
		request.setAttribute("type", tabTypes);
		return "update";
	}
}
